package com.yaolaizaixian.merchant.home;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.base.BaseInfo;
import com.yaolaizaixian.merchant.base.BaseModel;
import com.yaolaizaixian.merchant.bean.QuanyiInfo;
import com.yaolaizaixian.merchant.utils.GsonUtil;
import com.yaolaizaixian.merchant.utils.MyNetUtils;
import com.yaolaizaixian.merchant.utils.MyUtils;
import com.yaolaizaixian.merchant.utils.SharedPreferencesUtil;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class RightsDetailActivity extends BaseActivity {


    @BindView(R.id.ll_back)
    AutoLinearLayout llBack;
    @BindView(R.id.tv_title_text)
    TextView tvTitleText;
    @BindView(R.id.tv_rightsdetail_title)
    TextView tvRightsdetailTitle;
    @BindView(R.id.tv_rightsdetail_zhekou)
    TextView tvRightsdetailZhekou;
    @BindView(R.id.tv_rightsdetail_guize)
    TextView tvRightsdetailGuize;
    @BindView(R.id.tv_rightsdetail)
    TextView tvRightsdetail;

    private static final String TAG = "RightsDetailActivity";
    @BindView(R.id.tv_quanyi_time)
    TextView tvQuanyiTime;
    private Handler mhandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 1:

                    String result = (String) msg.obj;
                    Log.e(TAG, "权益详情" + result);
                    BaseModel baseModel5 = GsonUtil.jsonToBean(result, BaseModel.class);
                    if (baseModel5.isFlag()) {
                        QuanyiInfo quanyiInfo = GsonUtil.jsonToBean(result, QuanyiInfo.class);
                        if (null != quanyiInfo) {

                            String Shopname= SharedPreferencesUtil.getString(getApplicationContext(), "Shopname", "");
                            tvRightsdetailTitle.setText(Shopname+quanyiInfo.getData().getEr().getName());
                            tvRightsdetailZhekou.setText("权益内容:   "+quanyiInfo.getData().getEr().getName());
                            tvQuanyiTime.setText(MyUtils.getFirstr(quanyiInfo.getData().getEr().getBeginDate())+"-"+MyUtils.getFirstr(quanyiInfo.getData().getEr().getEditTime()));
                            tvRightsdetail.setText(quanyiInfo.getData().getEr().getRuleExplain());

                        }

                    } else {

                        //如果是账号被停用，就踢出到登录页面
                        if (baseModel5.getErrorCode().equals(BaseInfo.LogOutCode)) {

                            showConflictDialog(RightsDetailActivity.this, "账号被停用");

                        } else if (baseModel5.getErrorCode().equals(BaseInfo.LogOutCode1)) {

                            showConflictDialog(RightsDetailActivity.this, "您的账号在其它设备登录");

                        } else {

                            Toast.makeText(RightsDetailActivity.this, baseModel5.getErrorMsg(), Toast.LENGTH_SHORT);
                            return;

                        }

                    }

                    break;
            }
        }
    };


    @Override
    public void initView() {

        setContentView(R.layout.activity_rights_detail);
    }

    @Override
    public void initData() {

        tvTitleText.setText("权益详情");

        //获取权益ID
        String quanyiId=getIntent().getStringExtra("id");

        getQuanyiInfo(quanyiId);

    }

    private void getQuanyiInfo(String quanyiId) {
        try {

            //用户id
            HashMap<String, String> sortedParams = new HashMap<String, String>();
            final String url = BaseInfo.URL + "equityController/equityRuleInfo.do";
            sortedParams.put("equityRulesysno", quanyiId);
            OkHttpUtils
                    .post()
                    .url(url)
                    .params((HashMap) MyNetUtils.getInstance().getNetData(sortedParams, this))
                    .build()
                    .execute(new MyStringCallback());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick(R.id.ll_back)
    public void onClick() {

        finish();
    }


    class MyStringCallback extends StringCallback {
        @Override
        public void onError(Call call, Exception e, int id) {

            Log.e(TAG, e.toString());

        }

        @Override
        public void onResponse(String response, int id) {

            Message message = new Message();
            message.what = 1;
            message.obj = response;
            mhandler.sendMessage(message);
        }
    }

}
